package tech.aichan.AiQA.aop;

import tech.aichan.AiQA.annotation.TimeConsume;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Arrays;


/**
 * @author mengyu
 * @date 2024/03/03 9:31
 * 统计用时AOP
 */
@Aspect
@Component
@Slf4j
public class TimeConsumeAspect {

    @Around("@annotation(timeConsume)")
    public Object timeConsumeCheck(ProceedingJoinPoint pjp, TimeConsume timeConsume) throws Throwable {
        long before = System.currentTimeMillis();
        Object proceed = pjp.proceed();
        long after = System.currentTimeMillis();
        // 类名，方法名，参数
        // 方法名
        String methodName = pjp.getSignature().getName();
        // 类名
        String className = pjp.getSignature().getDeclaringType().getSimpleName();
        // 方法参数
        Object[] args = pjp.getArgs();
        log.info(className+"."+methodName+"({}),consume time:{}", Arrays.toString(args),after-before);
        return proceed;
    }

}
